<?php


namespace app\service\business;


use app\service\exception\BaseException;
use app\service\Y;
use EasyWeChat\Factory;

class Pay
{

    /**
     * 微信支付
     * @param $param
     * @return array|string
     * @throws BaseException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public static function wechat_pay($param)
    {
        $config = config('wechat.payment', []);
        $app = Factory::payment($config);

        $result = $app->order->unify([
            'body' => $param['body'],
            'out_trade_no' => $param['order_no'],
            'total_fee' => $param['total'],
            'notify_url' => $param['notify_url'], // 支付结果通知网址，如果不设置则会使用配置里的默认地址
            'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
            'openid' => $param['openid'],
        ]);

        $jssdk = $app->jssdk;
        if (isset($result['prepay_id']) && $result['prepay_id']) {
            $config = $jssdk->bridgeConfig($result['prepay_id'], false); // 返回数组
            return $config;
        } else {
            trace($result);
            throw new BaseException(['msg' => $result['err_code_des'] ?? '支付失败']);
        }
    }
}